package com.qf.dao.impl;

import com.qf.dao.UserDao;
import com.qf.model.User;
import com.qf.util.JDBCUtil;
import jdk.nashorn.internal.scripts.JD;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.Connection;

public class UserDaoImpl implements UserDao {
    QueryRunner qr=new QueryRunner();
    @Override
    public User SelectUserByUsername(String username) {
        Connection con=null;
        User u=null;
        try{
            con= JDBCUtil.getCon();
            String sql="select * from t_user where username=?";
            u = qr.query(con, sql, new BeanHandler<User>(User.class),username);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtil.colseCon(con);
        }

        return u;
    }

    @Override
    public int insertUser(User u) {
        Connection con=null;
        int a=0;
        try{
            con= JDBCUtil.getCon();
            String sql="insert into t_user (username,password,sex,email,code,status,role) values(?,?,?,?,?,?,?)";
           a = qr.update(con, sql, u.getUsername(), u.getPassword(), u.getSex(), u.getEmail(), u.getCode(), 0, u.getRole());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtil.colseCon(con);
        }

        return a;

    }
}
